package com.coollang.smartbra.utils;

import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.annotation.Nullable;

import com.coollang.smartbra.eventbus.CommonEvent;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/10/24 0024.
 */

public class GifUtils {

    public static void loadGif(SimpleDraweeView view, int id) {
        Uri uri = Uri.parse("res://" + PackageUtils.getAppPKName() + "/" + id);
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setControllerListener(controllerListener)
                .setAutoPlayAnimations(false) // 设置加载图片完成后是否直接进行播放
                .build();
        view.setController(draweeController);
    }

    static ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
        @Override
        public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
            if (anim != null) {
                // 其他控制逻辑
//                anim.start();
                EventBus.getDefault().post(new CommonEvent(anim, CommonEvent.REQUESTSUEECCD, CommonEvent.LOADFINISHED));
            }
        }
    };
}
